lower_table_n
)
## Combined table (lower part, variance terms)
lower_table_s <- stayer_info %>%
dplyr::select(model_id, starts_with("sigma_")) %>%
tidyr::pivot_longer(-model_id) %>%
dplyr::mutate(imp = dplyr::case_when(
grepl(".*_imp1", name) ~ 1L,
grepl(".*_imp2", name) ~ 2L,
grepl(".*_imp3", name) ~ 3L,
grepl(".*_imp4", name) ~ 4L,
grepl(".*_imp5", name) ~ 5L)
) %>%
dplyr::mutate(name = substr(name, 1, nchar(name) - 5L)) %>%
dplyr::group_by (model_id, name) %>%
dplyr::summarize(value = mean(value)) %>%
dplyr::mutate(
name = dplyr::case_when(
name == "sigma_res" ~ "$\\sigma_{\\text{Observations}}$",
name == "sigma_id" ~ "$\\sigma_{\\text{Individuals}}$",
name == "sigma_plz" ~ "$\\sigma_{\\text{Postcode areas}}$",
name == "sigma_kkz" ~ "$\\sigma_{\\text{Counties/cities}}$"
),
value = format(round(value, 3), nbig = 2, nsmall = 3)
) %>%
tidyr::pivot_wider(names_from = model_id) %>%
as.matrix()
## Combine
combined_table <- rbind(
as.matrix(upper_table),
c(
"$N$",
"\\makecell{ \\hspace{1mm} \\\\ \\hspace{1mm}}",
rep("", ncol(upper_table) - 2)
),
lower_table_n,
c(
"\\emph{Standard deviations of intercepts}",
"\\makecell{ \\hspace{1mm} \\\\ \\hspace{1mm}}",
rep("", ncol(upper_table) - 2)
),
lower_table_s
)
## TeX output
print(
xtable(
combined_table,
align = c("l", "l", "c", "c"),
label = "tab:reg1",
caption = paste0(
"Coefficients and simulation-based 95\\% confidence intervals from hierarchical linear within-between models, estimated across $M=5$ imputations."
)
),
include.rownames = FALSE,
include.colnames = TRUE,
sanitize.text.function = identity,
type = "latex",
hline.after = c(-1,
-1,
0,
which(combined_table[, "Variables"] == "\\emph{Mixed effects}") - 1,
which(combined_table[, "Variables"] == "\\emph{Between effects}") - 1,
which(combined_table[, "Variables"] == "$N$") - 1,
nrow(combined_table),
nrow(combined_table)),
size = "\\small",
comment = TRUE,
floating = FALSE,
tabular.environment = "longtable",
file = "tex/reg1_2024.tex"
)
source_log("04-03-main-analyses-tables.R")
sink()
sm(list=ls())
rm(list=ls())
# Source user-written functions ----
## Source functions
funs <- list.files("fun")
sapply(funs, function (file)
source(paste0("fun/", file)))
source_log("04-03-main-analyses-tables.R")
sink()
source_log("04-03-main-analyses-tables.R")
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
sink()
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
knitr::opts_chunk
knitr::opts_chunk()
knitr::opts_current()
knitr::opts_current
knitr::opts_current("fig.width")
?knitr::opts_chunk
?knitr::opts_chunk$get("fig.width")
knitr::opts_chunk$get("fig.width")
knitr::opts_chunk$get("fig.height")
sink()
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
sink()
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
3+3
sink()
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
3+3
# AfD-specific findings
stayer_ames <- rio::import("est/stayer_analyses_marginal_effects_main.csv")
stayer_desc <- rio::import("est/stayer_analyses_descriptives_main.csv")
stayer_info <-
rio::import("est/stayer_analyses_model_info_main.csv") %>%
dplyr::mutate(spec = dplyr::case_when(
is.na(spec) ~ "All",
spec == "ltr_since_3 == 0" ~ "Moved last 3",
spec == "ltr_since_5 == 0" ~ "Moved last 5",
spec == "ltr_since_3 == 1" ~ "Since 3",
spec == "ltr_since_5 == 1" ~ "Since 5"
))
View(stayer_ames)
min_x <- 5.410185
min_y <- -5.756471e-03
b <- c(0.002, 0.000)
b %*% c(1, min_x)
b <- c(0.002, -0.001)
b %*% c(1, min_x)
min_y
## Load objects
stayer_coef <- rio::import("est/stayer_analyses_model_coefficients_main.csv") %>%
dplyr::filter(model_id %in% c(254, 269)) %>%
dplyr::select(-V1)
stayer_desc <- rio::import("est/stayer_analyses_descriptives_main.csv") %>%
dplyr::filter(model_id %in% c(254, 269))
stayer_info <- rio::import("est/stayer_analyses_model_info_main.csv") %>%
dplyr::filter(model_id %in% c(254, 269))
## Harmonize variable names
colnames(stayer_coef) <- colnames(stayer_coef) %>%
gsub("X.", "", .) %>%
gsub("\\._", "\\_", .) %>%
gsub(".cold_rent_sqm_cwu_imp1.1", ".1.cold_rent_sqm_cwu_imp1", .) %>%
gsub(".cold_rent_sqm_cwu_imp2.1", ".1.cold_rent_sqm_cwu_imp2", .) %>%
gsub(".cold_rent_sqm_cwu_imp3.1", ".1.cold_rent_sqm_cwu_imp3", .) %>%
gsub(".cold_rent_sqm_cwu_imp4.1", ".1.cold_rent_sqm_cwu_imp4", .) %>%
gsub(".cold_rent_sqm_cwu_imp5.1", ".1.cold_rent_sqm_cwu_imp5", .) %>%
gsub(".cold_rent_sqm_umn_imp1.1", ".1.cold_rent_sqm_umn_imp1", .) %>%
gsub(".cold_rent_sqm_umn_imp2.1", ".1.cold_rent_sqm_umn_imp2", .) %>%
gsub(".cold_rent_sqm_umn_imp3.1", ".1.cold_rent_sqm_umn_imp3", .) %>%
gsub(".cold_rent_sqm_umn_imp4.1", ".1.cold_rent_sqm_umn_imp4", .) %>%
gsub(".cold_rent_sqm_umn_imp5.1", ".1.cold_rent_sqm_umn_imp5", .) %>%
gsub("gtyp5urban...500k_imp1.1", "gtyp5urban...500k.1_imp1", .) %>%
gsub("gtyp5urban...500k_imp2.1", "gtyp5urban...500k.1_imp2", .) %>%
gsub("gtyp5urban...500k_imp3.1", "gtyp5urban...500k.1_imp3", .) %>%
gsub("gtyp5urban...500k_imp4.1", "gtyp5urban...500k.1_imp4", .) %>%
gsub("gtyp5urban...500k_imp5.1", "gtyp5urban...500k.1_imp5", .)
## Recode x_names in stayer_coef (to match column names)
stayer_coef <- stayer_coef %>%
mutate(x_names = x_names %>%
as.character() %>%
ifelse(
startsWith(., "gtyp5urban > 500k"),
paste0(substr(., 1, nchar("gtyp5urban > 500k")),
".1",
substr(., nchar("gtyp5urban > 500k") + 1, nchar(.))),
.
)) %>%
mutate(x_names = x_names %>%
as.character() %>%
ifelse(endsWith(., "gtyp5urban > 500k"),
paste0(., ".1"),
.)) %>%
mutate(
x_names = x_names %>%
gsub("\\(", "", .) %>%
gsub("\\)", "", .) %>%
gsub("<", "\\.", .) %>%
gsub(">", "\\.", .) %>%
gsub("=", "\\.", .) %>%
gsub(" ", "\\.", .) %>%
gsub("\\:", "\\.", .) %>%
gsub("&", "\\.", .) %>%
gsub("-", "\\.", .) %>%
gsub("\\`", "", .)
)
## Split stayer_coef by model_id, drop empty columns
stayer_coef <- stayer_coef %>%
group_by(model_id) %>%
group_split %>%
lapply(select_if, all_na)
## Stack by imputations
stayer_est <- stayer_coef %>%
lapply(.,
function(mod) {
varying <- mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
split(
.,
mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
sapply(function(name)
substr(name, 1, nchar(name) - 5)) %>%
factor(
.,
levels = mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
sapply(function(name)
substr(name, 1, nchar(name) - 5)) %>%
unique()
)
) %>%
lapply(function(names)
sapply(names, function(name)
which(names(mod) == name)))
mod <- mod %>%
as.data.frame() %>%
reshape(
direction = "long",
varying = varying,
v.names = names(varying),
timevar = "imp",
times = 1:5
) %>%
dplyr::select(-id)
return(mod)
})
## Split by model_id and imp
stayer_est <- stayer_est %>%
lapply(.,
function(mod) {
mod %>%
group_by(imp) %>%
group_split()
})
## Simulate and combine
stayer_imp_sim <- stayer_est %>%
lapply(.,
function (mod) {
b_sim <- lapply(mod,
function (imp) {
x_names <- imp$x_names
b <- as.matrix(imp[, 4])
vcov <- as.matrix(imp[, 5:ncol(imp)])
vcov <- vcov[, x_names]
set.seed(20230329)
sim <- MASS::mvrnorm(10000L, b, vcov)
return(list(x_names = x_names,
sim = sim))
})
x_names <- b_sim[[1]]$x_names
b_sim <- lapply(b_sim, function (obj)
obj$sim) %>%
do.call(rbind, .) %>%
apply(., 2, quantile, c(.5, .025, .975)) %>%
round(3) %>%
format(nbig = 2, nsmall = 3) %>%
apply(., 2, function (x) {
paste0("\\makecell{", "$", x[1], "$ \\\\ $", "[", x[2], ",", x[3], "]$", "}")
}) %>% as.matrix()
b_sim <- cbind.data.frame(x_names,
b_sim)
})
View(stayer_imp_sim)
stayer_imp_sim[[1]]
stayer_est
str(stayer_est)
length(stayer_est)
stayer_est[[1]] %>% length()
stayer_est[[1]][[1]]
stayer_est[[2]][[1]]
View(stayer_coef)
stayer_coef[[2]][[1]]
stayer_coef[[2]]
## Simulate and combine
stayer_imp_sim <- stayer_est %>%
lapply(.,
function (mod) {
b_sim <- lapply(mod,
function (imp) {
x_names <- imp$x_names
b <- as.matrix(imp[, 4])
vcov <- as.matrix(imp[, 5:ncol(imp)])
vcov <- vcov[, x_names]
set.seed(20230329)
sim <- MASS::mvrnorm(10000L, b, vcov)
return(list(x_names = x_names,
sim = sim))
})
x_names <- b_sim[[1]]$x_names
b_sim <- lapply(b_sim, function (obj)
obj$sim) %>%
do.call(rbind, .) %>%
apply(., 2, quantile, c(.5, .025, .975)) %>%
round(6) %>%
format(nbig = 2, nsmall = 6) %>%
apply(., 2, function (x) {
paste0("\\makecell{", "$", x[1], "$ \\\\ $", "[", x[2], ",", x[3], "]$", "}")
}) %>% as.matrix()
b_sim <- cbind.data.frame(x_names,
b_sim)
})
stayer_imp_sim[[2]]
b <- c(-0.018129, 0.000978)
b %*% (1, x_min)
b %*% c(1,  x_min)
b %*% c(1, min_x)
min_y
## Load objects
stayer_coef <- rio::import("est/stayer_analyses_model_coefficients_main.csv") %>%
dplyr::filter(model_id %in% c(209, 224)) %>%
dplyr::select(-V1)
stayer_desc <- rio::import("est/stayer_analyses_descriptives_main.csv") %>%
dplyr::filter(model_id %in% c(209, 224))
stayer_info <- rio::import("est/stayer_analyses_model_info_main.csv") %>%
dplyr::filter(model_id %in% c(209, 224))
## Harmonize variable names
colnames(stayer_coef) <- colnames(stayer_coef) %>%
gsub("X.", "", .) %>%
gsub("\\._", "\\_", .) %>%
gsub(".cold_rent_sqm_cwu_imp1.1", ".1.cold_rent_sqm_cwu_imp1", .) %>%
gsub(".cold_rent_sqm_cwu_imp2.1", ".1.cold_rent_sqm_cwu_imp2", .) %>%
gsub(".cold_rent_sqm_cwu_imp3.1", ".1.cold_rent_sqm_cwu_imp3", .) %>%
gsub(".cold_rent_sqm_cwu_imp4.1", ".1.cold_rent_sqm_cwu_imp4", .) %>%
gsub(".cold_rent_sqm_cwu_imp5.1", ".1.cold_rent_sqm_cwu_imp5", .) %>%
gsub(".cold_rent_sqm_umn_imp1.1", ".1.cold_rent_sqm_umn_imp1", .) %>%
gsub(".cold_rent_sqm_umn_imp2.1", ".1.cold_rent_sqm_umn_imp2", .) %>%
gsub(".cold_rent_sqm_umn_imp3.1", ".1.cold_rent_sqm_umn_imp3", .) %>%
gsub(".cold_rent_sqm_umn_imp4.1", ".1.cold_rent_sqm_umn_imp4", .) %>%
gsub(".cold_rent_sqm_umn_imp5.1", ".1.cold_rent_sqm_umn_imp5", .) %>%
gsub("gtyp5urban...500k_imp1.1", "gtyp5urban...500k.1_imp1", .) %>%
gsub("gtyp5urban...500k_imp2.1", "gtyp5urban...500k.1_imp2", .) %>%
gsub("gtyp5urban...500k_imp3.1", "gtyp5urban...500k.1_imp3", .) %>%
gsub("gtyp5urban...500k_imp4.1", "gtyp5urban...500k.1_imp4", .) %>%
gsub("gtyp5urban...500k_imp5.1", "gtyp5urban...500k.1_imp5", .)
## Recode x_names in stayer_coef (to match column names)
stayer_coef <- stayer_coef %>%
mutate(x_names = x_names %>%
as.character() %>%
ifelse(
startsWith(., "gtyp5urban > 500k"),
paste0(substr(., 1, nchar("gtyp5urban > 500k")),
".1",
substr(., nchar("gtyp5urban > 500k") + 1, nchar(.))),
.
)) %>%
mutate(x_names = x_names %>%
as.character() %>%
ifelse(endsWith(., "gtyp5urban > 500k"),
paste0(., ".1"),
.)) %>%
mutate(
x_names = x_names %>%
gsub("\\(", "", .) %>%
gsub("\\)", "", .) %>%
gsub("<", "\\.", .) %>%
gsub(">", "\\.", .) %>%
gsub("=", "\\.", .) %>%
gsub(" ", "\\.", .) %>%
gsub("\\:", "\\.", .) %>%
gsub("&", "\\.", .) %>%
gsub("-", "\\.", .) %>%
gsub("\\`", "", .)
)
## Split stayer_coef by model_id, drop empty columns
stayer_coef <- stayer_coef %>%
group_by(model_id) %>%
group_split %>%
lapply(select_if, all_na)
## Stack by imputations
stayer_est <- stayer_coef %>%
lapply(.,
function(mod) {
varying <- mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
split(
.,
mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
sapply(function(name)
substr(name, 1, nchar(name) - 5)) %>%
factor(
.,
levels = mod %>%
dplyr::select(contains("_imp")) %>%
names() %>%
sapply(function(name)
substr(name, 1, nchar(name) - 5)) %>%
unique()
)
) %>%
lapply(function(names)
sapply(names, function(name)
which(names(mod) == name)))
mod <- mod %>%
as.data.frame() %>%
reshape(
direction = "long",
varying = varying,
v.names = names(varying),
timevar = "imp",
times = 1:5
) %>%
dplyr::select(-id)
return(mod)
})
## Split by model_id and imp
stayer_est <- stayer_est %>%
lapply(.,
function(mod) {
mod %>%
group_by(imp) %>%
group_split()
})
## Simulate and combine
stayer_imp_sim <- stayer_est %>%
lapply(.,
function (mod) {
b_sim <- lapply(mod,
function (imp) {
x_names <- imp$x_names
b <- as.matrix(imp[, 4])
vcov <- as.matrix(imp[, 5:ncol(imp)])
vcov <- vcov[, x_names]
set.seed(20230329)
sim <- MASS::mvrnorm(10000L, b, vcov)
return(list(x_names = x_names,
sim = sim))
})
x_names <- b_sim[[1]]$x_names
b_sim <- lapply(b_sim, function (obj)
obj$sim) %>%
do.call(rbind, .) %>%
apply(., 2, quantile, c(.5, .025, .975)) %>%
round(3) %>%
format(nbig = 2, nsmall = 3) %>%
apply(., 2, function (x) {
paste0("\\makecell{", "$", x[1], "$ \\\\ $", "[", x[2], ",", x[3], "]$", "}")
}) %>% as.matrix()
b_sim <- cbind.data.frame(x_names,
b_sim)
})
## Simulate and combine
stayer_imp_sim <- stayer_est %>%
lapply(.,
function (mod) {
b_sim <- lapply(mod,
function (imp) {
x_names <- imp$x_names
b <- as.matrix(imp[, 4])
vcov <- as.matrix(imp[, 5:ncol(imp)])
vcov <- vcov[, x_names]
set.seed(20230329)
sim <- MASS::mvrnorm(10000L, b, vcov)
return(list(x_names = x_names,
sim = sim))
})
x_names <- b_sim[[1]]$x_names
b_sim <- lapply(b_sim, function (obj)
obj$sim) %>%
do.call(rbind, .) %>%
apply(., 2, quantile, c(.5, .025, .975)) %>%
round(6) %>%
format(nbig = 2, nsmall = 6) %>%
apply(., 2, function (x) {
paste0("\\makecell{", "$", x[1], "$ \\\\ $", "[", x[2], ",", x[3], "]$", "}")
}) %>% as.matrix()
b_sim <- cbind.data.frame(x_names,
b_sim)
})
stayer_imp_sim[[2]]
b <- c(0.002316, -0.001491)
b %*% c(1, min_x)
# Load packages ----
#install.packages("pacman")
pacman::p_load(
dplyr,
tidyr,
magrittr,
sf,
zoo,
readxl,
readr,
lme4,
MASS,
plm,
fixest,
foreign,
Amelia,
sjmisc,
snow,
parallel,
ggplot2,
ggpubr,
gridExtra,
reshape2,
xtable,
marginaleffects,
margins,
splines,
viridis
)
# Source user-written functions ----
## Source functions
funs <- list.files("fun")
sapply(funs, function (file)
source(paste0("fun/", file)))
# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
